﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DC2010.Objects.Info;
using DC2010.Objects;
using DC2010;
using DC2010.Tools;
using DC2010_Editor.Formulars;
using System.Diagnostics;
using DC2010_Editor.Properties;
using DC2010.Objects.Base;

namespace DC2010_Editor.Controls
{
    public partial class DoorUC : UserControl
    {
        private Door data { get; set;}

        public DoorUC()
        {
            InitializeComponent();
        }

        private void DoorUC_Load(object sender, EventArgs e)
        {
            Refresh();
        }

        public void SetControls()
        {
            int a = data.Info.OpenPosition;

            DoorIdTextBox.Text = data.Info.Id;
            BasedOnTextBox.Text = data.Info.TemplateId;
            OpenPositionNumericUpDown.Text = data.Info.OpenPosition.ToString();
            OpenTresholdNumericUpDown.Value = data.Info.OpenThreshold;
            OpenDirectionComboBox.SelectedIndex = data.Info.OpenDirection;
            IsBrokenCheckBox.Checked = data.Info.IsBroken;
            IsPassableCheckBox.Checked = data.Info.IsPassable;
            comboBox1.Text = data.Gfx.MainTex;
            inventoryTextBox1.SetRelatedObject(data.Info.Keys);
            ConsumeKeyCheckBox.Checked = data.Info.ConsumeKey;
        }

        public void GetFromControls()
        {
            data.Info.Id = DoorIdTextBox.Text;
            data.Info.TemplateId = BasedOnTextBox.Text;
            data.Info.OpenPosition = (int)OpenPositionNumericUpDown.Value;
            data.Info.OpenThreshold = (int)OpenTresholdNumericUpDown.Value;
            data.Info.OpenDirection = OpenDirectionComboBox.SelectedIndex;
            data.Info.IsBroken = IsBrokenCheckBox.Checked;
            data.Info.IsPassable = IsPassableCheckBox.Checked;
            data.Gfx.MainTex = comboBox1.Text;
            data.Info.ConsumeKey = ConsumeKeyCheckBox.Checked;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //instance
            if (Root.I.DoorSystem.ListInstance == null) Root.I.DoorSystem.ListInstance = new List<Door>();

            if (!Root.I.DoorSystem.ListInstance.Contains(data))
            {
                Root.I.DoorSystem.ListInstance.Add(data);
            }

            Root.I.DoorSystem.SaveInstance();
            Root.I.DoorSystem.LoadInstance();

            Refresh();
        }

        private void Refresh()
        {
            TemplatesImageListBox.Items.Clear();
            DungeonImageListBox.Items.Clear();

            foreach (Door d in Root.I.DoorSystem.List)
            {
                ImageListBoxItem itm = new ImageListBoxItem(d.Info.Id, Resources.Core, d);
                TemplatesImageListBox.Items.Add(itm);
            }

            foreach (Door d in Root.I.DoorSystem.ListInstance)
            {
                BaseObject c = d.FindOwner();
                if (c == null)
                {
                    ImageListBoxItem itm = new ImageListBoxItem(d.Info.Id, Resources.Instanced, d);
                    DungeonImageListBox.Items.Add(itm);
                }
                else
                {
                    ImageListBoxItem itm = new ImageListBoxItem(d.Info.Id, Resources.InstancedWithOwner, d);
                    DungeonImageListBox.Items.Add(itm);
                }
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            Root.I.DoorSystem.LoadInstance();
            Refresh();
        }

        private void imageListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ImageListBox ilb = (ImageListBox)sender;

            if (ilb.SelectedItem != null)
            {
                if (ilb.SelectedItem is ImageListBoxItem)
                {
                    ImageListBoxItem ilbi = (ImageListBoxItem)ilb.SelectedItem;

                    if (ilbi.Tag is Door)
                    {
                        data = (Door)ilbi.Tag;
                        SetControls();
                    }
                }
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            //noone was messing with that
            string id = string.Format(DoorIdTextBox.Text, Editor.I.DoorsCount);
            string tempId = BasedOnTextBox.Text;

            Door d = new Door();
            d.Info.Id = id;
            d.Info.TemplateId = tempId;

            Root.I.DoorSystem.AddToSystem(d, id, ObjectTarget.Core);
            Root.I.DoorSystem.Save();

            Refresh();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //noone was messing with that
            string id = string.Format(DoorIdTextBox.Text, Editor.I.DoorsInstanceCount);
            string tempId = BasedOnTextBox.Text;

            Door d = new Door();
            d.Info.Id = id;
            d.Info.TemplateId = tempId;

            Root.I.DoorSystem.AddToSystem(d, id, ObjectTarget.Instance);
            Root.I.DoorSystem.SaveInstance();

            Refresh();
            
        }

        private void button7_Click(object sender, EventArgs e)
        {
            GetFromControls();

            Root.I.DoorSystem.Save();
            Root.I.DoorSystem.SaveInstance();
        }

        private void crystalButton5_Click(object sender, EventArgs e)
        {
            if (data != null)
            {
                Root.I.DoorSystem.MoveToC(data.Info.Id);
            }

            Refresh();
        }

        private void crystalButton6_Click(object sender, EventArgs e)
        {
            if (data != null)
            {
                Root.I.DoorSystem.MoveToC(data.Info.Id);
            }

            Refresh();
        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void crystalButton8_Click(object sender, EventArgs e)
        {
            /// Remove from system.
            Root.I.DoorSystem.RemoveFromSystem(data, ObjectTarget.Instance);

            Door i = Root.I.IS.GetDoor(data.TemplateId);
            Door iNew = (Door)i.Clone();

            string id = data.Id;
            string tempId = BasedOnTextBox.Text;

            iNew.Info.Id = id;
            iNew.Info.TemplateId = i.Info.Id;

            Root.I.DoorSystem.AddToSystem(iNew, id, ObjectTarget.Instance);
            Root.I.DoorSystem.SaveInstance();
        }

        private void crystalButton7_Click(object sender, EventArgs e)
        {
            if (data != null)
            {
                data.Id = data.Info.Id;
                Root.I.DoorSystem.RemoveFromSystem(data, ObjectTarget.Core);
                Root.I.DoorSystem.RemoveFromSystem(data, ObjectTarget.Instance);
            }

            Refresh();
        }

    }
}
